# need to set iptables first (via commandline I believe)
# sudo apt-get install python-scapy
# sudo apt-get install -y nfqueue-bindings-python
# sudo modprobe nfnetlink_queue
# iptables -A FORWARD -p tcp -j NFQUEUE 

##interceptor.py
import sys
#from socket import *
import nfqueue
from scapy.all import * 
import os

if len(sys.argv) == 1:
    FILE = "output.txt"
else:
    FILE = sys.argv[1]

#file = open(FILE, 'w+')
#function to handle run() of nfqueue
def cb(dummy, payload):
   print 'in callback function, payload data below'

   print "payload len ", payload.get_length()
   data =  payload.get_data()
   # s = struct.Struct(data)
   # unpacked = s.unpack_from(data, 0)
   pkt = IP(payload.get_data())
   
   try:
       file = open(FILE, 'a')
       crack = str(pkt.load)
       print crack
       file.write(crack)
       file.write('\n')
       file.close()
   except AttributeError:
       pass# I've chosen to put the info into a file instead of trying to decrypt in this callback function
    # use later decrypting... message = pkt.decode(encoding='UTF-8') 

def main():
    q = None
    q = nfqueue.queue()
    q.open()
    
   # q.unbind(socket.AF_INET)
    q.bind(socket.AF_INET)
    q.set_callback(cb)
    q.create_queue(0)

    print 'nfqueue created'
    try:
        print 'entering try_run()'
        q.try_run()
        print 'after try_run()'
    except KeyboardInterrupt:
        print "Exiting..." 
        q.unbind(socket.AF_INET)
        q.close()
        try:
            print 'trying to close file'
 #           file.close()
        except TypeError:
            print 'TypeError with file close'

main()
